<?php
namespace Admin\Controller;
use Think\Controller;

class CustomController extends HomeController {
	/*
	* Function:		customOrderList
	* Description:	定制订单列表
	*/
    public function customOrderList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 10;
		$filter = returnFilter($filter);
		$list = D('CustomOrders')->getCustomOrdersList($page,$filter,$pageSize);
		if(is_array($list)&&count($list)>0){
		    foreach($list as &$val){
		        if(!empty($val['user_id'])){
					$userInfo = D('User')->getUserInfo($val['user_id']);
					$val['username'] = emojiDecode($userInfo['username']);
					$val['avatarurl'] = $userInfo['avatarurl'];
				}
		    }
		}
		$count =  D('CustomOrders')->getCustomOrdersCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
		$this->assign('list',$list);
        $orderStatusList = D('CustomOrders')->getOrderStatusList();
        $this->assign('orderStatusList',$orderStatusList);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$this->display();
    }

    //订单详情
    public function customOrderDetail(){
        $id = I('id','');
        $customInfo = D('CustomOrders')->getCustomOrdersInfo($id);
        if(!empty($customInfo['user_id'])){
			$userInfo = D('User')->getUserInfo($customInfo['user_id']);
			$customInfo['username'] = emojiDecode($userInfo['username']);
		}
        $itemsList = M('custom_order_list')->where(['tid'=>$customInfo['id']])->select();
        if(is_array($itemsList)&&count($itemsList)>0){
            foreach($itemsList as &$val){
    			if($info = M('custom_cate')->where(['id'=>$val['cate_id']])->find()){
                    $pInfo = M('custom_cate')->where(['id'=>$info['pid']])->find();
                    $val['type'] = $pInfo['cate_name'];
                    $val['value'] = $info['cate_name'];
                }
            }
        }
		$this->assign('info',$customInfo);
		$this->assign('itemsList',$itemsList);
		$this->display();
    }

    //完成订单
    public function completeOrder($id = ''){
        if($orderInfo = D('CustomOrders')->getCustomOrdersInfo($id)){
		    if($orderInfo['status']!=0){
		        $ret = array('code'=>0,'message'=>'状态异常!');
			    $this->ajaxReturn($ret);
		    }
			$ret = M('custom_orders')->where(['id'=>$id])->save(['status'=>2]);
			if($ret!==false){
			    $ret = array('code'=>1,'message'=>'操作成功!');
			    $this->ajaxReturn($ret);
			}else{
			    $ret = array('code'=>0,'message'=>'操作失败!');
			    $this->ajaxReturn($ret);
			}
		}else{
			$ret = array('code'=>0,'message'=>'不存在此定制订单!');
			$this->ajaxReturn($ret);
		}
    }

    //取消订单
    public function cancelOrder($id = ''){
        if($orderInfo = D('CustomOrders')->getCustomOrdersInfo($id)){
		    if($orderInfo['status']!=0){
		        $ret = array('code'=>0,'message'=>'状态异常!');
			    $this->ajaxReturn($ret);
		    }
			$ret = M('custom_orders')->where(['id'=>$id])->save(['status'=>2]);
			if($ret!==false){
			    $ret = array('code'=>1,'message'=>'操作成功!');
			    $this->ajaxReturn($ret);
			}else{
			    $ret = array('code'=>0,'message'=>'操作失败!');
			    $this->ajaxReturn($ret);
			}
		}else{
			$ret = array('code'=>0,'message'=>'不存在此定制订单!');
			$this->ajaxReturn($ret);
		}
    }

	/*
	* Function:		customCateList
	* Description:	定制分类列表
	*/
    public function customCateList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 100;
		$filter = returnFilter($filter);
		$list = D('CustomCate')->getCustomCateList($page,$filter,$pageSize);
		if(is_array($list)&&count($list)>0){
			foreach($list as &$val){
				$val['second_list'] = M('custom_cate')->where(['pid'=>$val['id'],'status'=>1,'is_deleted'=>0])->order('sort desc,id desc')->select();
			}
		}
		$count =  D('CustomCate')->getCustomCateCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$this->display();
    }
	
	/*
	* Function:		addCustomCate
	* Description:	添加定制分类
	*/
	public function addCustomCate(){
		$ret = array();
		if(IS_POST){
			$data = I('post.');
			$data['addtime'] = date('Y-m-d H:i:s',time());
			if(D('CustomCate')->addCustomCate($data)){
				$ret = array('code'=>1,'message'=>'添加成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'添加失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			$cateList = D('CustomCate')->getCustomCateAllList(0);
			$this->assign('cateList',$cateList);
			$this->display();
		}
	}
	
	/*
	* Function:		editCustomCate
	* Description:	编辑定制分类
	* @param:		$id int id
	*/
	public function editCustomCate($id = ''){
		if(IS_POST){
			$data = I('post.');
			$ret = D('CustomCate')->updateCustomCate($data);
			if($ret!==false){
				$ret = array('code'=>1,'message'=>'更新成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'更新失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			if(empty($id)){
				$this->error('参数异常');
			}else{
				$info = D('CustomCate')->getCustomCateInfo($id);
				$this->assign('info',$info);
				if($info['pid']!=0){
					$cateList = D('CustomCate')->getCustomCateAllList(0);
					$this->assign('cateList',$cateList);
				}
				$this->display();
			}
		}
	}
}